package com.nuaa.community.util;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

/**
 * ClassName: MailClient
 * Package: com.nuaa.community.util
 * Description:发送邮件的工具类
 */
@Component
public class MailClient {

    private static final Logger logger = LoggerFactory.getLogger(MailClient.class);

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    public void sendMail(String to, String subject, String content){
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setFrom(from); // 发件人
            helper.setTo(to); // 收件人
            helper.setSubject(subject);
            helper.setText(content, true); // 发送的文本，支持html文件
            mailSender.send(helper.getMimeMessage()); // 发送邮件
        } catch (MessagingException e) {
            logger.error("发送邮件失败："+ e.getMessage());
        }
    }
}
